package com.csusm.geo.science.tutor;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.location.Location;
import android.util.Log;

public class LocationDbHelper {
	public ArrayList<LocationsDb> getLinks(Location L) {
		String result = null;
		ArrayList<LocationsDb> retVal = new ArrayList<LocationsDb>();
		InputStream is = null;
		StringBuilder sb = null;
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("Latitude", Double.toString(L
				.getLatitude())));
		nameValuePairs.add(new BasicNameValuePair("Longitude", Double
				.toString(L.getLongitude())));

		// http post
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
					"http://ouyangdev.cs.csusm.edu/GeoTutor/getlocation.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection" + e.toString());
		}
		// convert response to string
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			sb = new StringBuilder();
			sb.append(reader.readLine() + "\n");

			String line = "0";
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result = sb.toString();
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}
		// Parse the string

		String[] rowsTemp;
		/* delimiter */
		String rdelimiter = ":::;::";
		String cdelimiter = ":::::";
		if (result != null) {
			/* given string will be split by the argument delimiter provided. */
			
			result = result.trim();
			rowsTemp = result.split(rdelimiter);
			/* print substrings */
			for (int i = 0; i < rowsTemp.length; i++) {
				LocationsDb ldb = new LocationsDb();
				System.out.println("location coordinates---"+rowsTemp[i]);
				String[] tempcolumn;
				String temprow = rowsTemp[i];
				if (rowsTemp[i] != null) {
					tempcolumn = temprow.split(cdelimiter);
					for (int j = 0; j < tempcolumn.length; j++) {
						// System.out.println("column " + j + " ::::" +
						// tempcolumn[j]);
						if (j == 0 && tempcolumn[j] != null) {
							ldb.setPlace(tempcolumn[j]);
						}
						if (j == 1 && tempcolumn[j] != null) {
							ldb.setLat(tempcolumn[j]);
						}
						if (j == 2 && tempcolumn[j] != null) {
							ldb.setLon(tempcolumn[j]);
						}

					}
					retVal.add(ldb);
				}
			}
		}
		return retVal;
	}
}
